package com.shop.service.impl;

import com.shop.entity.Product;
import com.shop.service.CartService;
import com.shop.service.ProductService;
import com.shop.service.vo.CartVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ProductService productService;
    @Override
    public CartVo changeCart(Integer productId, Integer userId, Integer count){
        Product product = productService.getById(productId);
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        CartVo cartVo = (CartVo) opsForHash.get(userId.toString(), productId.toString());
        if (count == 1){
            if (cartVo == null){
                cartVo = new CartVo();
                cartVo.setQuantity(1)
                        .setTotal(product.getProductPrice())
                        .setPrice(product.getProductPrice())
                        .setProductName(product.getProductName())
                        .setProductImgSrc(product.getProductImgsrc())
                        .setProductId(product.getId());
                opsForHash.put(userId.toString(),productId.toString(),cartVo);
                return cartVo;
            }
            cartVo.setQuantity(cartVo.getQuantity()+1)
                    .setTotal(cartVo.getTotal().add(product.getProductPrice()));

        }else if(count == -1){
            if (cartVo == null){
                return null;
            }
            cartVo.setQuantity(cartVo.getQuantity() -1).setTotal(cartVo.getTotal().subtract(product.getProductPrice()));
        }else{
            if (cartVo == null){
                return null;
            }
            cartVo.setQuantity(0);
        }
        opsForHash.put(userId.toString(),productId.toString(),cartVo);
        if (cartVo.getQuantity() == 0){
            opsForHash.delete(userId.toString(),productId.toString());
        }

        return cartVo;
    }

    @Override
    public BigDecimal calculateTotal(Integer userId, Integer[] productIds) {
        List<CartVo> cartVos = searchCartByIds(userId, productIds);
        BigDecimal totalPrice = BigDecimal.ZERO;
        for(CartVo item : cartVos){
            totalPrice = totalPrice.add(item.getTotal());
        }

        return totalPrice;
    }
    @Override
    public List<CartVo> searchCartByIds(Integer userId, Integer[] productIds){
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        List<Object> collect = Arrays.stream(productIds).map(Object::toString).collect(Collectors.toList());
        List<Object> productList =  opsForHash.multiGet(userId.toString(), collect);
        return productList.stream().map(o -> (CartVo) o).collect(Collectors.toList());
    }
}
